package de.koller.planet2D.frame.uv;

import de.koller.oceanFramework.tree.Child;
import de.koller.planet2D.PNode;
import de.koller.planet2D.container.TextureValue;
import de.koller.planet2D.frame.listener.ChangeListener;
import de.koller.planet2D.frame.previewRender.Camera;
import de.koller.planet2D.rcom.RemovePNode;
import de.koller.planet2D.util.Parsers;

public class UVDesigner extends PNode {

	@Child public final Camera camera = new Camera();
	public final UVDesignerComp comp = new UVDesignerComp( this );
	private final ChangeListener changeListener = new ChangeListener( this );
	
	private final TextureValue textureValue;

	public UVDesigner( TextureValue textureValue ) {
		this.textureValue = textureValue;
	}
	
	@Override
	protected void init() {
		super.init();
		camera.scale = 1;
		camera.pos.set( 0, 0 );
		
		comp.setVisible( true );
		comp.x.getDocument().addDocumentListener( changeListener );
		comp.y.getDocument().addDocumentListener( changeListener );
		comp.w.getDocument().addDocumentListener( changeListener );
		comp.h.getDocument().addDocumentListener( changeListener );
	}

	@Override
	protected void release() {
		super.release();
		
		comp.dispose();
	}

	@Override
	protected void render( int elapsedTime ) {
		super.render( elapsedTime );
		
		comp.renderPanel.repaint();
	}
	
	public void closeThis() {
		this.proot.call( new RemovePNode( this ) );
	}
	
	public void generateUV() {
		textureValue.x = (int)Parsers.getValue( 0, comp.x.getText() );
		textureValue.y = (int)Parsers.getValue( 0, comp.y.getText() );
		textureValue.w = (int)Parsers.getValue( 0, comp.w.getText() );
		textureValue.h = (int)Parsers.getValue( 0, comp.h.getText() );
		closeThis();
	}
	
}
